package com.yuantong.xyerp.rest.controller;

import com.yuantong.xyerp.core.api.enums.EnumMsgCode;
import com.yuantong.xyerp.core.api.model.Area;
import com.yuantong.xyerp.core.api.model.base.RestResponseModel;
import com.yuantong.xyerp.core.api.service.IAreaService;
import com.yuantong.xyerp.core.api.vo.AreaFullVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/area")
@Api(tags = "省市区数据接口")
public class AreaController {
    @Autowired
    private IAreaService areaService;

    @GetMapping("/provinces")
    @ApiOperation(value = "获取省列表",httpMethod = "GET",notes = "无需参数")
    public RestResponseModel<List<Area>> listProvinces() {
        return RestResponseModel.SUCCESS(EnumMsgCode.MSG_SUCCESS, areaService.listProvinces());
    }


    @GetMapping("/cities")
    @ApiOperation(value = "获取市列表",httpMethod = "GET",notes = "根据省code获取省下市列表，提取省code前两位匹配")
    public RestResponseModel<List<Area>> listCities(@ApiParam("省code（提取前两位，后面字符忽略）") @RequestParam String provinceCode) {
        if (StringUtils.isBlank(provinceCode)) {
            return RestResponseModel.SUCCESS(EnumMsgCode.MSG_SUCCESS, Collections.emptyList());
        }

        return RestResponseModel.SUCCESS(EnumMsgCode.MSG_SUCCESS, areaService.listCities(provinceCode));
    }

    @GetMapping("/districts")
    @ApiOperation(value = "获取区列表",httpMethod = "GET",notes = "根据市code获取市下的县区列表，提取市code前四位匹配")
    public RestResponseModel<List<Area>> listDistricts(@ApiParam("市code（提取前四位，后面字符忽略）") @RequestParam String cityCode) {
        if (StringUtils.isBlank(cityCode)) {
            return RestResponseModel.SUCCESS(EnumMsgCode.MSG_SUCCESS, Collections.emptyList());
        }

        return RestResponseModel.SUCCESS(EnumMsgCode.MSG_SUCCESS, areaService.listDistricts(cityCode));
    }
    @GetMapping("/full")
    @ApiOperation(value = "获取区域完整信息",httpMethod = "GET",notes = "根据6位完整的区域code获取对应省市区的全部信息，完全对应上返回信息，否则属性全空")
    public RestResponseModel<AreaFullVo> areaFullInfo(@ApiParam("6位区域code码") @RequestParam String areaCode) {
        if (StringUtils.isBlank(areaCode)) {
            return RestResponseModel.SUCCESS(EnumMsgCode.MSG_SUCCESS, new AreaFullVo());
        }

        return RestResponseModel.SUCCESS(EnumMsgCode.MSG_SUCCESS, areaService.getFullArea(areaCode));
    }

}
